package merge

import (
	"fmt"
	"game/game-design-v.xnanz/gen"
	"os"
	"strings"
)

type Catalog struct {
	Class []string
}

type CatalogClass struct {
	Name string
}

func CatalogFun(args []string) {
	catalogArgs := NewCatalogArgs(args)

	fileList, err := os.ReadDir(catalogArgs.SourceHome)
	if err != nil {
		fmt.Println(err)
		return
	}
	var catalog Catalog
	catalog.Class = make([]string, 0)
	for _, f := range fileList {
		if f.IsDir() {
			continue
		}
		if !strings.HasSuffix(f.Name(), ".ts") {
			continue
		}
		dotIndex := strings.Index(f.Name(), ".")
		// var clazz CatalogClass
		// clazz.Name = f.Name()[:dotIndex]
		catalog.Class = append(catalog.Class, f.Name()[:dotIndex])
	}
	fmt.Println(catalog)
	lines := gen.ReadTemplateFile(catalogArgs.TemplateFile)

	gen.Params["catalog"] = &catalog

	classBlocks := gen.NewClassBlock(lines)
	fmt.Println("OOOOOO", len(classBlocks))

	for _, clazz := range classBlocks {

		os.Mkdir(clazz.FileDir, 0755)
		content := clazz.BuildResult()

		fmt.Println("导出代码=>\t", clazz.FileName)
		err := os.WriteFile(clazz.FileDir+clazz.FileName, []byte(content), 0666)
		if err != nil {
			fmt.Println(err)
		}
	}

}
